import {asyncRoutes} from '@/router'
import router from '@/router'
import Main from '@/pages/main'

/**
 * 过滤路由
 * */
function filterAsyncRoutes(allRouter, userRouter) {
    const realRoutes = [
        {
            path: '/main',
            name: 'View',
            component: Main,
            redirect: '/home',
            children: []
        }
    ]
    allRouter.forEach(v => {
        userRouter.forEach(item => {
            if (v.meta.title === item.name) {
                realRoutes[0].children.push(v)
            } else if (item.items && item.items.length) {
                item.items.forEach(item2 => {
                    if (v.meta.title == item2.name) {
                        realRoutes[0].children.push(v)
                    }
                })
            }
        })
    })
    return realRoutes

}

const state = {
    menuList: []
}

const mutations = {
    setMenuList(menuList) {
        state.menuList = menuList
    }
}

const actions = {
    generateRoutes({commit}, roles) {
        router.addRoutes(filterAsyncRoutes(asyncRoutes, roles))
    },
    getMenuList() {
        return state.menuList
    },

}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
